<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use Illuminate\Support\Facades\DB;

class CarSubsidy extends CxhshopBase {
    protected $table = 'ims_cxh_apply_subsidy_car';
    protected $fillable = [];
    public $timestamps = false;

    /**
     * 添加车辆补贴
     * @param $memId
     * @param array $params
     * @return int
     */
    public function addCarApply($memId,$params=[],$uuid,$refuseCarId=0){
        $query = self::query();
        if($params['re_add'] == 1){//重新上传
            $changeData = [
                'is_del' => 1,
            ];
            $query ->where('id',$refuseCarId)-> update($changeData);
        }
      $saveData = [
          'aplc_car_no'=> $params['car_no'],
          'aplc_car_type'=> $params['car_type'],
          'aplc_car_vin'=> $params['car_vin'],
          'member_id'=> $memId,
          'member_uuid'=> $uuid,
          'aplc_car_one' => $params['aplc_car_one'],//车身照一
          'aplc_car_two' => $params['aplc_car_two'],//车身照二
          'ctime' => time(),
          'utime' => time(),
      ];
        $subsidyId = $query -> insertGetId($saveData);
        return $subsidyId;
    }

    /**
     * @param $params
     * @return array
     * 查询当前申请的车辆是否已申请
     */
    public function getCarInfo($params,$arr= []){
        $query = self::query();
        $sql = '(aplc_car_no='."'".$params['car_no']."'".' or aplc_car_vin='."'".$params['car_vin']."')";
        $info = $query
            ->whereIn('aplc_status',$arr)
            ->whereRaw($sql)
            ->first(['id','aplc_car_no','aplc_car_vin','aplc_car_type','aplc_status']);
        if(empty($info)){
            $data = [];
        }else{
            $data = $info -> toArray();
        }
        return $data;
    }

    /**
     * 获取车辆补贴详情
     * @param int $id 车辆补贴id
     * @return array
     */
    public function getCarDetail($id=0,$memId = 0,$fields){
        $query = self::query();
        $detail = $query -> where('id',$id) -> where('member_id',$memId)-> first($fields);
        if(empty($detail)){
            $data = [];
        }else{
            $data = $detail -> toArray();
        }
        return $data;
    }

    /**
     * 删除该辆车补
     * @param $id 车辆补贴id
     * @return int
     */
    public function delCar($id){
        $query = self::query();
        $saveData = ['is_del'=>1];
        $res = $query -> where('id',$id) -> update($saveData);
        return $res;
    }
    /**
     * 根据修改车辆补贴相关字段
     */
    public function updateCarFields($saveData = [],$carId = 0){
        $res = self::query() -> where('id',$carId) -> update($saveData);
        return $res;
    }

    /**
     * 获取当前用户车辆补贴列表
     * @param $uuid
     * @param array $field
     * @return array
     */
    public function getListByUuid($uuid,$field = ['*']){
        $list = self::query() -> where('member_uuid',$uuid) -> where('is_del',0) -> orderBy('id','desc') -> get($field);
        if($list){
            $data = $list -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }
}